package dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import dao.mapper.PersonRowMapper;
import domainmodel.Person;

public class DerByDao implements IDao {

	private DataSource dataSource;
	
	@Override
	public void setDataSource(DataSource ds) {
		this.dataSource = ds;
	}

	@Override
	public void create(String firstName, String lastName) {
		JdbcTemplate insert = new JdbcTemplate(dataSource);
		insert.update("INSERT into MYKILLERAPP.PERSON (FIRSTNAME, LASTNAME) VALUES(?, ?)", new Object[] { firstName, lastName});
	}

	@Override
	public List<Person> select(String firstName, String lastName) {
		JdbcTemplate select = new JdbcTemplate(dataSource);
		return select.query("select FIRSTNAME, LASTNAME from MYKILLERAPP.PERSON where FIRSTNAME = ? AND LASTNAME = ?", 
				new Object[] {firstName, lastName}, 
				new PersonRowMapper());
	}

	@Override
	public List<Person> selectAll() {
		JdbcTemplate select = new JdbcTemplate(dataSource);
		return select.query("select FIRSTNAME, LASTNAME from MYKILLERAPP.PERSON", new PersonRowMapper());
	}

	@Override
	public void deleteAll() {
		JdbcTemplate delete = new JdbcTemplate(dataSource);
		delete.update("DELETE from MYKILLERAPP.PERSON");
	}

	@Override
	public void delete(String firstName, String lastName) {
		JdbcTemplate delete = new JdbcTemplate(dataSource);
		delete.update("DELETE from MYKILLERAPP.PERSON where FIRSTNAME = ? AND LASTNAME = ?", new Object[] {firstName, lastName});
	}

}
